package MyList;

public class mylist implements IList {

        static class ListNode{
                public int val;
                public ListNode next;
                public ListNode prev;
                public ListNode(int val){
                        this.val  = val;
                }
        }
        public ListNode head;
        public ListNode last;


        @Override
        public void IList() {

        }
        @Override
        public void display() {
             ListNode cur = head;
                while(cur!=null)
                {
                        System.out.print(cur.val+" ");
                        cur = cur.next;
                }
                System.out.println();
        }
        @Override
        public void addFirst(int data) {
                ListNode node = new ListNode(data);
                node.next = this.head;
                this.head=node;
        }

        @Override
        public void addLast(int data) {
                ListNode node = new ListNode(data);
                if(head == null)
                {
                        head = node;
                }
                else{
                      ListNode cur = head;
                      while(cur.next!=null)
                      {
                              cur = cur.next;
                      }
                      cur.next = node;
                }

        }

        @Override
        public void contain(int search) {
                ListNode cur = head;
                int index = 0;
                while(cur.val!=search)
                {
                        cur = cur.next;
                        if(cur==null)
                        {
                                System.out.println("链表中没有这个元素");
                                break;
                        }
                        index++;
                }
                System.out.println("找到了该元素，元素下标为："+index);
        }

        @Override
        public void indexOf(int index) {

        }

        @Override
        public void get() {
                mylist list = new mylist();

        }

        @Override
        public void set() {

        }

        @Override
        public void remove(int key) {
                ListNode cur = head;
                while (cur != null) {
                        if(cur.val == key) {
                                if(cur == head) {
                                        head = head.next;
                                        if(head != null) {
                                                head.prev = null;
                                        }else {
                                                //只有一个节点 且是需要删除的节点
                                                last = null;
                                        }
                                }else {
                                        cur.prev.next = cur.next;
                                        //删除中间节点
                                        if(cur.next != null) {
                                                cur.next.prev = cur.prev;
                                                //cur.prev.next = cur.next;
                                        }else {
                                                //删除尾巴节点
                                                //cur.prev.next = cur.next;
                                                last = last.prev;
                                        }
                                }
                                return;
                        }
                        cur = cur.next;
                }
        }

        @Override
        public int size() {
                ListNode cur = head;
                int length = 0;
                if(head==null)
                return 0;
                else {
                        while (cur!=null)
                        {
                               cur = cur.next;
                               length++;
                        }
                }
                return length;
        }

        @Override
        public void clear() {
                ListNode cur = head;
                while(cur!=null)
                {
                        cur = null;

                }
        }

        @Override
        public boolean isempty() {
                return true;
        }

        @Override
        public boolean isfull() {
                return false;
        }

        public static void main(String[] args) {
            mylist list = new mylist();
            list.addLast(12);
            list.addLast(34);
            list.addLast(56);
            list.addLast(78);
            list.remove(78);


        }
}
